const express = require('express')
const app = express()
const cors = require('cors')
const chalk = require('chalk')
const bodyParser = require('body-parser')
const token = require('./src/middleware/token/token')
const { host, port } = require('./config')

const {
    custom: {
        shopCustom,
        shopCart,
        swiperCustom,
        categoryCustom,
        brandCustom,
        user,
        address,
        orderCustom,
        commentCustom,
        messageCustom,
        footprint
    },
    admin: {
        shopAdmin,
        swiperAdmin,
        userAdmin,
        admin,
        categoryAdmin,
        brandAdmin,
        orderAdmin,
        addressAdmin,
        commentAdmin,
        messageAdmin,
        statistic
    },
    upload
} = require('./src/router')

app.use(cors())
app.use(express.json({ limit: '50mb' }))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extend: false }))
app.use(token)

app.use('/api/user', user)
app.use('/api/cart', shopCart)
app.use('/api/banner', swiperCustom)
app.use('/api/category', categoryCustom)
app.use('/api/brand', brandCustom)
app.use('/api/pro', shopCustom)
app.use('/api/address', address)
app.use('/api/order', orderCustom)
app.use('/api/comment', commentCustom)
app.use('/api/message', messageCustom)
app.use('/api/footprint', footprint)

app.use('/admin/user', userAdmin)
app.use('/admin/admin', admin)
app.use('/admin/banner', swiperAdmin)
app.use('/admin/category', categoryAdmin)
app.use('/admin/brand', brandAdmin)
app.use('/admin/pro', shopAdmin)
app.use('/admin/address', addressAdmin)
app.use('/admin/order', orderAdmin)
app.use('/admin/comment', commentAdmin)
app.use('/admin/message', messageAdmin)
app.use('/admin/statistic', statistic)

app.use('/up', upload)

app.use('/static', express.static('server'))

app.listen(port, () => {
    console.log(chalk.green('服务器已经启动，端口' + port), chalk.cyan(host + '/static'))
})
